package p.ithorns.integration.hazelcast.mqtt;

import p.ithorns.framework.common.utils.StringUtil;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: liuhaitao
 * @Date: 2022/8/18 8:55
 * @Description: 主题
 */
public enum MqttUpTopic {

    // 空
    NONE("", BaseRaw.class),

    /**
     * 上传数据
     */
    UPLOAD("upload/{0}/data", GateUp.class),

    ;

    private final static Map<String, MqttUpTopic> MAP = Arrays.stream(MqttUpTopic.values())
            .collect(Collectors.toMap(MqttUpTopic::getTopic, v -> v, (v1, v2) -> v2));

    /**
     * 主题
     */
    private final String topic;

    /**
     * 消息类型
     */
    private final Class<? extends BaseRaw> rawClass;

    MqttUpTopic(String topic, Class<? extends BaseRaw> rawClass) {
        this.topic = topic;
        this.rawClass = rawClass;
    }

    public static MqttUpTopic of(String topic) {
        topic = StringUtil.trimAllWhitespace(topic);
        if (StringUtil.isBlank(topic)) {
            return NONE;
        }

        return Optional
                .ofNullable(MAP.get(topic))
                .orElse(NONE);
    }

    public String getTopic() {
        return topic;
    }

    public Class<? extends BaseRaw> getRawClass() {
        return rawClass;
    }

}